package com.example.teesystem.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
/**
 * @author punch
 * @data 2021-09-27 11:03
 */
public class TimeUtil {

    /**
     * 获取当前的秒数
     * @return
     */
    public static int getSecondTimestamp(){
        String time=String.valueOf(System.currentTimeMillis());
        return Integer.parseInt(time.substring(0,time.length()-3));
    }
    /**
     * 获取当前的毫秒数
     */
    public static long getTimestamp(){
        String time=String.valueOf(System.currentTimeMillis());
        return Long.parseLong(time);
    }
    /**
     * 将String转换成LocalDate类型，否则返回2000，1，1
     * @param str
     * @return
     */
    public static LocalDate stringToLocalDate(String str){
        LocalDate localDate=LocalDate.of(2000,1,1);
        if(str == null|| str.equals("")) {
            return localDate;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        try {
            return LocalDate.parse(str, df);
        } catch (Exception e){
            return localDate;
        }
    }
    /**
     * 将LocalDate转成String
     * @param localDate
     * @return
     */
    public static String localDateToString(LocalDate localDate){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        try {
            return localDate.format(df);
        } catch (Exception e){
            return "2000年1月1日";
        }
    }
    /**
     * 将LocalDateTime转成String
     * @param localDateTime
     * @return
     */
    public static String localDateTimeToSting(LocalDateTime localDateTime){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        try {
            return localDateTime.format(df);
        }catch (Exception e){
            return "error";
        }
    }
    /**
     * 将String转成localdatetime
     * @param str
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String str){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        try{
            return LocalDateTime.parse(str,df);
        }catch (Exception e){
            return null;
        }
    }
    public static String timeLongToString(String time){
        String res;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        long lt = new Long(time);
        //将时间戳转换为时间

        Date date = new Date(lt);
        //将时间调整为yyyy-MM-dd HH:mm:ss时间样式

        res = simpleDateFormat.format(date);
        System.out.println(res);
        return res;
    }
    /**
     * 将Date转成String
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");//日期格式
        String time = sformat.format(date);

        return time;
    }

    /**
     * 将Date转成String
     * @param date
     * @return
     */
    public static String dateTimeToString(Date date) {
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");//日期格式
        String time = sformat.format(date);

        return time;
    }
    /**
     * 将Date转成String
     * @param date
     * @return
     */
    public static Date stringToDate(String date) throws ParseException {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = ft.parse(date);
        return date1;
    }

    public static int differentDays(Date date1,Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) {//同一年
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else {// 不同年
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }
}
